/*
 * sqldb.cpp
 *
 *  Created on: 2010-10-28
 *      Author: kaishui
 */
#include "sqldb.h"



bool myDatabase::open(string host,string user,string password,string dbname)
{

	conn=mysql_init(NULL);
	if(!conn)
	{
		throw mysql_Exception(conn);
		return false;
	}
	if(NULL==mysql_real_connect(conn,host.c_str(),user.c_str(),password.c_str(),dbname.c_str(),0,NULL,0))
	{
		// throw mysql_Exception(conn);
		 return false;
	}
	mysql_set_character_set(conn, "utf8");
	mysql_autocommit(conn,true);
	pDB=this;
	return true;
}

void myDatabase::close()
{
	mysql_close(conn);
}

bool myDatabase::exec(const string str)
{
    int ret=mysql_query(conn,str.c_str());
	if(ret)
	{
		throw mysql_Exception(conn);
		return false;
	}
	return true;
}



//myRecordset



myRecordSet::myRecordSet(myDatabase* pDB)
{
	if(!pDB)
			throw nullpoint_Exception(GET_NAME(pDB));
	if(!pDB->conn)
	{
		throw nullpoint_Exception(GET_NAME(pDB->conn));

	}
	conn=pDB->conn;
}

bool myRecordSet::open(const string str)
{

	int ret=mysql_real_query(conn,str.c_str(),str.length());
	if(ret)
	{
		throw mysql_Exception(conn);
		return false;
	}


	result=mysql_store_result(conn);
	if(!result)
	{
		throw mysql_Exception(conn);
		return false;
	}


//	cout<<mysql_num_rows(result)<<endl;
	row = mysql_fetch_row(result);

/*	int rownum=mysql_num_rows(result);
	cout<<"result row ="<<rownum<<endl;*/


	getFieldCount();
	return true;
}

bool myRecordSet::isEOF()
{
//	return mysql_eof( result);
	if(row) return false;
	return true;
}


void myRecordSet::close()
{

	mysql_free_result(result);
}



void myRecordSet::moveNext()
{
	row = mysql_fetch_row(result);
}

unsigned int myRecordSet::getFieldCount()
{
	filed_num=mysql_num_fields(result);
	return filed_num;
}

bool myRecordSet::getFieldValue(unsigned int nIndex, bool& bValue)
{
	assert(nIndex<filed_num);
	if(!row[nIndex])
	{
			bValue=false;
			return false;
	}
	sscanf(row[nIndex],"%c",(char *)&bValue);
	return true;
}


bool myRecordSet::getFieldValue(unsigned int nIndex, short int& nValue)
{
	assert(nIndex<filed_num);
	if(!row[nIndex])
	{
			nValue=0;
			return false;
	}
	sscanf(row[nIndex],"%d",(int *)&nValue);
	return true;
}


bool myRecordSet::getFieldValue(unsigned int nIndex,unsigned short int &uValue)
{
	assert(nIndex<filed_num);
	if(!row[nIndex])
	{
			uValue=0;
			return false;
	}
	sscanf(row[nIndex],"%u",(unsigned int *)&uValue);
	return true;
}

bool myRecordSet::getFieldValue(unsigned int nIndex, int& nValue)
{
	assert(nIndex<filed_num);
	if(!row[nIndex])
	{
			nValue=0;
			return false;
	}
	sscanf(row[nIndex],"%d",&nValue);
	return true;
}


bool myRecordSet::getFieldValue(unsigned int nIndex,unsigned int &uValue)
{
	assert(nIndex<filed_num);

	if(!row[nIndex])
	{
			uValue=0;
			return false;
	}
	sscanf(row[nIndex],"%u",&uValue);
	return true;
}

bool myRecordSet::getFieldValue(unsigned int nIndex, long& lValue)
{
	assert(nIndex<filed_num);
	if(!row[nIndex])
	{
			lValue=0;
			return false;
	}
	sscanf(row[nIndex],"%ld",&lValue);
	return true;
}


bool myRecordSet::getFieldValue(unsigned int nIndex,unsigned long& ulValue)
{
	assert(nIndex<filed_num);
	if(!row[nIndex])
	{
			ulValue=0;
			return false;
	}
	sscanf(row[nIndex],"%lu",&ulValue);
	return true;
}


bool myRecordSet::getFieldValue(unsigned int nIndex, string& sValue)
{
	assert(nIndex<filed_num);
	if(!row[nIndex])
	{
			sValue="";
			return false;
	}
	sValue=row[nIndex];
	return true;
}


bool myRecordSet::getFieldValue(unsigned int nIndex, double& dbValue)
{
	assert(nIndex<filed_num);
	if(!row[nIndex])
	{
			dbValue=0;
			return false;
	}
	sscanf(row[nIndex],"%lf",&dbValue);
	return true;
}


int myRecordSet::getFieldIndex(string fieldName)
{
	unsigned int i;
	for(i = 0; i < filed_num; i++)
	{
	   field = mysql_fetch_field_direct(result, i);
	   if (strcasecmp(field->name,fieldName.c_str())==0)
		   break;
	}
	if(i>= filed_num)  return -1;
	return i;
}


bool myRecordSet::getFieldValue(string fieldName, bool& bValue)
{
	int index=getFieldIndex(fieldName);
	if(index<0) return false;
	return getFieldValue(index,bValue);
}

bool myRecordSet::getFieldValue(string fieldName, short int& nValue)
{
	int index=getFieldIndex(fieldName);
	if(index<0) return false;
	return getFieldValue(index,nValue);
}

bool myRecordSet::getFieldValue(string fieldName, unsigned short int& uValue)
{
	int index=getFieldIndex(fieldName);
	if(index<0) return false;
	return getFieldValue(index,uValue);
}


bool myRecordSet::getFieldValue(string fieldName, int& nValue)
{
	int index=getFieldIndex(fieldName);
	if(index<0) return false;
	return getFieldValue(index,nValue);
}

bool myRecordSet::getFieldValue(string fieldName, unsigned int& uValue)
{
	int index=getFieldIndex(fieldName);
	if(index<0) return false;
	return getFieldValue(index,uValue);
}


bool myRecordSet::getFieldValue(string fieldName, long& lValue)
{
	int index=getFieldIndex(fieldName);
	if(index<0) return false;
	return getFieldValue(index,lValue);
}


bool myRecordSet::getFieldValue(string fieldName, unsigned long& ulValue)
{
	int index=getFieldIndex(fieldName);
	if(index<0) return false;
	return getFieldValue(index,ulValue);
}


bool myRecordSet::getFieldValue(string fieldName, string& sValue)
{
	int index=getFieldIndex(fieldName);
	if(index<0) return false;
	return getFieldValue(index,sValue);
}


bool myRecordSet::getFieldValue(string fieldName, double& dbValue)
{
	int index=getFieldIndex(fieldName);
	if(index<0) return false;
	return getFieldValue(index,dbValue);
}
